স্প্রিং জেডিবিসি (Spring JDBC) তে EhCache এবং Redis এর সাথে ইন্টিগ্রেশন একটি কার্যকরী পদ্ধতি হতে পারে, যেহেতু ক্যাশিং ডেটাবেস অ্যাক্সেসের সংখ্যা কমায়, কর্মক্ষমতা বৃদ্ধি করে এবং দ্রুত অ্যাক্সেস নিশ্চিত করে। ক্যাশিং ব্যবহারের মাধ্যমে অ্যাপ্লিকেশন আরও দ্রুত এবং স্কেলেবল হয়, কারণ ক্যাশে হওয়া ডেটা পুনরায় ডেটাবেস থেকে না এনে সরাসরি ক্যাশ থেকে রিট্রিভ করা যায়।
EhCache হল একটি জনপ্রিয়, ওপেন সোর্স ক্যাশিং লাইব্রেরি যা ডেটা মেমোরি-ভিত্তিক স্টোরেজে সংরক্ষণ করে, যা অ্যাপ্লিকেশনের কর্মক্ষমতা বৃদ্ধি করতে সহায়তা করে।
<dependencies>
<!-- Spring Cache Dependency -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.3.20</version>
</dependency>
<!-- EhCache Dependency -->
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.9.1</version>
</dependency>
</dependencies>
এখন EhCache কনফিগার করার জন্য স্প্রিং কনফিগারেশন ফাইল ব্যবহার করতে হবে। এখানে একটি উদাহরণ দেওয়া হলো।
EhCache কনফিগারেশন (XML Config)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-3.0.xsd">
<!-- Enable caching in Spring -->
<cache:annotation-driven />
<!-- EhCache Configuration -->
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<constructor-arg ref="ehCache" />
</bean>
<bean id="ehCache" class="org.ehcache.core.EhcacheManager">
<constructor-arg value="classpath:ehcache.xml" />
</bean>
</beans>
EhCache কনফিগারেশন (ehcache.xml)
<ehcache xmlns="http://www.ehcache.org/v3">
<cache alias="employeeCache">
<key-type>java.lang.Integer</key-type>
<value-type>com.example.Employee</value-type>
<resources>
<heap unit="entries" value="1000" />
<offheap unit="MB" value="10" />
</resources>
</cache>
</ehcache>
এখন, আপনি @Cacheable
অ্যানোটেশন ব্যবহার করে স্প্রিং জেডিবিসি মেথডে ক্যাশিং প্রয়োগ করতে পারেন।
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
@Component
public class EmployeeDao {
@Cacheable(value = "employeeCache", key = "#id")
public Employee getEmployeeById(int id) {
// Database query for fetching employee by id
String sql = "SELECT * FROM Employee WHERE id = ?";
return jdbcTemplate.queryForObject(sql, new Object[]{id}, new BeanPropertyRowMapper<>(Employee.class));
}
}
ব্যাখ্যা:
@Cacheable
অ্যানোটেশনটি EmployeeDao
ক্লাসে ব্যবহৃত হয়েছে যাতে যখন ডেটাবেসে কোনো কর্মচারীর তথ্য অনুসন্ধান করা হয়, তখন প্রথমবার এটি ডেটাবেস থেকে আসবে এবং পরবর্তীতে ক্যাশ থেকে তা রিটার্ন করা হবে।Redis একটি ইন-মেমরি ডেটাবেস, যা মূলত ডেটা স্টোরেজ, ক্যাশিং, এবং মেসেজ ব্রোকার হিসেবে ব্যবহৃত হয়। Redis স্প্রিং অ্যাপ্লিকেশনে একটি জনপ্রিয় ক্যাশিং সমাধান হিসেবে ব্যবহৃত হয়।
<dependencies>
<!-- Spring Data Redis Dependency -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.5.6</version>
</dependency>
<!-- Lettuce Redis Client Dependency -->
<dependency>
<groupId>io.lettuce.core</groupId>
<artifactId>lettuce-core</artifactId>
<version>6.0.1</version>
</dependency>
</dependencies>
স্প্রিং অ্যাপ্লিকেশন কনফিগারেশনে Redis সেটআপ করতে হবে।
RedisConfig.java:
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.redis.RedisCacheManager;
import org.springframework.cache.redis.RedisCacheConfiguration;
import org.springframework.cache.redis.RedisCache;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.connection.RedisConnectionFactory;
@Configuration
@EnableCaching
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<String, Object> template = new StringRedisTemplate(connectionFactory);
return template;
}
@Bean
public CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
RedisCacheManager cacheManager = RedisCacheManager.create(connectionFactory);
return cacheManager;
}
}
এখন, আপনি @Cacheable
অ্যানোটেশনটি Redis ক্যাশিং ব্যবহারের জন্য ব্যবহার করতে পারেন।
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
@Component
public class EmployeeDao {
@Cacheable(value = "employeeCache", key = "#id")
public Employee getEmployeeById(int id) {
// Database query for fetching employee by id
String sql = "SELECT * FROM Employee WHERE id = ?";
return jdbcTemplate.queryForObject(sql, new Object[]{id}, new BeanPropertyRowMapper<>(Employee.class));
}
}
ব্যাখ্যা:
@Cacheable
অ্যানোটেশনটি ব্যবহার করা হয়েছে, যা Redis ক্যাশে employeeCache
নামক ক্যাশে ডেটা সংরক্ষণ করবে। এটি ক্যাশে থেকে ডেটা ফিরিয়ে দেয় এবং যদি ডেটা ক্যাশে না থাকে তবে ডেটাবেস থেকে নিয়ে আসে।উপরোক্ত উদাহরণ এবং কনফিগারেশন অনুসরণ করে আপনি স্প্রিং জেডিবিসি অ্যাপ্লিকেশনে EhCache বা Redis সঠিকভাবে ইন্টিগ্রেট করতে পারবেন, যা আপনার অ্যাপ্লিকেশনকে দ্রুত এবং স্কেলেবল করবে।
Read more